package mess;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class LeeCode_77 {
    public static void main(String[] args) {
        combine(4, 3);
    }

    private static List<List<Integer>> combine(int n, int k) {
        List<List<Integer>> res = new ArrayList<>();
        backTrick(res, new ArrayList<>(), n, k);
        return res;
    }

    private static void backTrick(List<List<Integer>> res, List<Integer> list, int n, int k){
        int len = list.size();
        if(len != 0 && n - list.get(len - 1) + len < k)
            return;
        if(len == k){
            for (Integer integer : list) {
                System.out.print(integer + " ");
            }
            System.out.println();
            res.add(list);
            return;
        }
        if(len == 0){
            for(int i = 1; i <= n; i++){
                List<Integer> newList = new ArrayList<>(list);
                newList.add(i);
                backTrick(res, newList, n, k);
            }
            return;
        }
        int last = list.get(len - 1);
        for(int i = last + 1; i <= n; i++){
            List<Integer> newList = new ArrayList<>(list);
            newList.add(i);
            backTrick(res, newList, n, k);
        }
    }
}
